package com.vote.controllers;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Post;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.vote.model.User;
import com.vote.services.UserService;
import com.vote.utils.HostHelper;

public class LoginController {

	@Autowired
	private UserService userService;

	public String index(Invocation inv) {
		return "login";
	}

	@Post("loginIn")
	public String loginIn(Invocation inv, @Param("userName")
	String userName, @Param("passWord")
	String passWord) {
		User user = new User();
		user.setUserName(userName);
		user.setUserPassword(passWord);
		user = userService.login(user);
		JSONObject jo = new JSONObject();
		HostHelper.setHost(inv.getRequest(), user);
		if (user != null)
		{
			jo.put("code", 1);
		} else {
			jo.put("code", 0);
		}
		return String.format("@%s", jo.toString());
	}
	
	@Post("loginOut")
	public String loginOut(Invocation inv)
	{
		HostHelper.removeHost(inv.getRequest());
		JSONObject jo = new JSONObject();
		jo.put("code", 1);
		return String.format("@%s", jo.toString());
	}
}
